<?php

namespace app\admin\model;

use think\Model;
use think\Db;

class UserModel extends Model {

    public $results = array('status' => 'error', 'msg' => '', 'data' => '');

    function user_list($key = "") {
        $pageParam['query']['key'] = $key;
        if ($key) {
            $where['user_name'] = ['like', "%$key%"];
            $where['email'] = ['like', "%$key%"];
        }
        $rs = Db::table('user')->whereOr($where)->paginate(10, FALSE, $pageParam);
        if (!$rs) {
            return $this->results;
        }
        $this->results['status'] = 'success';
        $this->results['data'] = $rs;
        return $this->results;
    }

    function user_info($id) {
        $where['id'] = $id;
        return Db::table('user')->where($where)->find();
    }

    function change_user($data) {
        $where['id'] = $data["id"];
        $rs = Db::table('user')->where($where)->update($data);
        return $rs;
    }

    function del_user($id) {
        $where['id'] = $id;
        return Db::table('user')->where($where)->delete();
    }

    /**
     * 封号 /解封
     * @param type $user_id
     * @param type $state
     */
    function banned($user_id, $state) {
        if ($state == 1) {
            $up['status'] = "1";
        } else {
            $up['status'] = "0";
        }
        return Db::table('user')->where('id', $user_id)->update($up);
    }

}
